package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.guoguojie.domain.BonusApply;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.DoingPictorial;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.dao.BonusDao;
import cn.sumpu.app.guoguojie.domain.Bonus;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class BonusDaoImpl extends SqlSessionDaoSupport implements BonusDao {

	private static Logger logger = LoggerFactory.getLogger(BonusDaoImpl.class);
	
	//添加红利
	@Override
	public void addBonus(Bonus bonus) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.addBonus",bonus);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addBonus database failure.", e);
			}
			throw new AppBusinessException("addBonus database failure.", e);
		}
	}
	//更新红利
	@Override
	public void updateBonusById(Bonus bonus) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.updateBonusById",bonus);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateBonusById database failure.", e);
			}
			throw new AppBusinessException("updateBonusById database failure.", e);
		}
	}
	//修改红利申请
	@Override
	public void updateBonusApplyById(BonusApply bonusApply)
			throws AppBusinessException {
		try {
			 getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.updateBonusApplyById",bonusApply);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateBonusApplyById database failure.", e);
			}
			throw new AppBusinessException("updateBonusApplyById database failure.", e);
		}
	}
	//删除红利
	@Override
	public void deleteBonusById(String bonusId) throws AppBusinessException {
		try {
			 getSqlSession().delete(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.deleteBonusById",bonusId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteBonusById database failure.", e);
			}
			throw new AppBusinessException("deleteBonusById database failure.", e);
		}
	}
	//在后台查询红利申请的会员
	@SuppressWarnings("unchecked")
	@Override
	public List<BonusApply> getBonusApplyAll(Map<String, Object> map)
			throws AppBusinessException {
		try {
			 return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getBonusApplyAll",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBonusApplyAll database failure.", e);
			}
			throw new AppBusinessException("getBonusApplyAll database failure.", e);
		}
	}
	//在后台查询红利申请的会员总量
	@Override
	public Integer getBonusApplyCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			 return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getBonusApplyCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBonusApplyCount database failure.", e);
			}
			throw new AppBusinessException("getBonusApplyCount database failure.", e);
		}
	}
	
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Bonus> getBonusList(Map<String,Object> map) throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getBonusList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@Override
	public int getBonusListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getBonusListCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Member> getApplyMember(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getApplyMember",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Doing> getDoingByBonus(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getDoingByBonus",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@Override
	public int getDoingByBonusCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getDoingByBonusCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@Override
	public void updateViewAmount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			 getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.updateViewAmount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}
	
	
	@Override
	public void updateApplyNums(Map<String, Object> map)
			throws AppBusinessException {
		try {
			 getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.updateApplyNums",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<BonusApply> getBonusApply(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getBonusApply",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@Override
	public void insertBonusApply(BonusApply bonusApply)
			throws AppBusinessException {
		try {
			 getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.insertBonusApply",bonusApply);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Bonus> getCanApply(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getCanApply",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<BonusApply> getApplyNow(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getApplyNow",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@Override
	public Bonus getIndexBonus() throws AppBusinessException {
		try {
			return (Bonus) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getIndexBonus");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<DoingPictorial> getIndexBonusReply() throws AppBusinessException {
		try {
			 return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.BonusMapper.getIndexBonusReply");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getIndexBonusReply database failure.", e);
			}
			throw new AppBusinessException("getIndexBonusReply database failure.", e);
		}
	}
	
	
}
